package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/lab-online/internal/course/constant"
	"github.com/lab-online/internal/course/entity"
	mw "github.com/lab-online/pkg/middleware"
	"github.com/lab-online/pkg/resp"
	"github.com/lab-online/pkg/util"
)

type searchExperimentReqQuery struct {
	ID   uint   `form:"id" binding:"omitempty,min=1" example:"1"`             // 实验ID
	Name string `form:"name" binding:"omitempty,min=1,max=10" example:"高等数学"` // 实验名称
}

var SearchExperimentValidator = mw.NewValidator(func() *mw.ValidatorOptions {
	return &mw.ValidatorOptions{
		Query: &searchExperimentReqQuery{},
	}
})

type SearchExperimentRespData struct {
	ID          uint   `json:"id" binding:"required"`          // 实验ID
	Name        string `json:"name" binding:"required"`        // 实验名称
	Description string `json:"description" binding:"required"` // 实验描述
}

type SearchExperimentRespBody struct {
	resp.BaseRespBody
	Data []SearchExperimentRespData `json:"data" binding:"required"`
}

// SearchExperiment 搜索实验
//
//	@Summary				搜索实验
//	@Description.markdown	SearchCourse
//	@Tags					course
//	@Accept					json
//	@Produce				json
//	@Security				bearer
//	@Param					Authorization	header		string						true	"`all`"
//	@Param					experiment		query		searchExperimentReqQuery	true	"实验信息"
//	@Success				200				{object}	SearchExperimentRespBody
//	@Failure				400				{object}	resp.FailedRespBodyString
//	@x-apifox-status		"developing"
//	@Router					/v1/experiments	[get]
func (h *Handler) SearchExperiment(c *gin.Context) {
	query := c.MustGet(mw.KeyQuery).(*searchExperimentReqQuery)
	experimentEntity := entity.NewExperimentEntity(
		entity.WithExpID(query.ID),
		entity.WithExpName(query.Name),
	)

	if experiments, err := h.domain.SearchExperiment(experimentEntity); err != nil {
		resp.ErrorHandler(c, err)
	} else {
		resp.Success(c, constant.CodeSearchExperimentSuccess, util.SliceMap(experiments,
			func(experiment entity.ExperimentEntity) *SearchExperimentRespData {
				return &SearchExperimentRespData{
					ID:          experiment.GetID(),
					Name:        experiment.GetName(),
					Description: experiment.GetDescription(),
				}
			}),
		)
	}
}
